﻿namespace AnyOffice.Components
{
    using log4net;
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Xml;
    using System.Reflection;

    public class AnyOfficeConfigHandler : IConfigurationSectionHandler
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public virtual object Create(object parent, object context, XmlNode section)
        {
            Hashtable hashtable = CollectionsUtil.CreateCaseInsensitiveHashtable();
            this.InitConfig(hashtable);
            this.GetConfig(parent, context, section, hashtable, "anyoffice");
            return hashtable;
        }

        private void GetConfig(object parent, object context, XmlNode node2, Hashtable config, string prefix)
        {
            foreach (XmlNode node in node2.ChildNodes)
            {
                if (node.NodeType.Equals(XmlNodeType.Element))
                {
                    string section = prefix + "/" + node.Name;
                    SingleTagSectionHandler handler = new SingleTagSectionHandler();
                    this.GetSection(config, section, handler.Create(parent, context, node) as Hashtable);
                }
            }
        }

        private void GetSection(Hashtable config, string section, Hashtable configsection)
        {
            foreach (DictionaryEntry entry in configsection)
            {
                string str = (section + "/" + entry.Key).ToLower();
                config[str] = this.GetValue(entry.Value.ToString(), config[str]);
            }
        }

        private object GetValue(string key, object value)
        {
            if (key == null) return value;
            try
            {
                if (value.GetType().Equals(typeof(int))) return int.Parse(key);
                if (value.GetType().Equals(typeof(bool))) return bool.Parse(key);
                return key;
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
                return value;
            }
        }

        private void InitConfig(Hashtable hashtable)
        {
            hashtable.Add("anyoffice/global/administrator_privid", 1);
            hashtable.Add("anyoffice/global/ajax_interval", 0x2bf20);
            hashtable.Add("anyoffice/global/attach_maxsize", 0x1e00000);
            hashtable.Add("anyoffice/global/imageupload_maxsize", 0x7d000);
            hashtable.Add("anyoffice/global/page_size", 10);
            hashtable.Add("anyoffice/global/sqlfor_rescallback", string.Empty);
            hashtable.Add("anyoffice/global/sqlfor_userdelete", string.Empty);
            hashtable.Add("anyoffice/global/news_num", 5);
            hashtable.Add("anyoffice/global/notify_num", 5);
            hashtable.Add("anyoffice/global/email_num", 5);
            hashtable.Add("anyoffice/global/vote_num", 5);
            hashtable.Add("anyoffice/global/bbs_num", 5);
            hashtable.Add("anyoffice/global/rtx_enabled", false);
            hashtable.Add("anyoffice/global/skype_button", false);
        }
    }
}

